#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/wait.h>

void PrintPending(sigset_t &pending)
{
    std::cout << "curr process[" << getpid() << "]pending: ";
    for (int signo = 31; signo >= 1; signo--)
    {
        if (sigismember(&pending, signo))
        {
            std::cout << 1;
        }
        else
            std::cout << 0;
    }
    std::cout << "\n";
}

// void handler(int signo)
// {
//     std::cout << "信号被递达！！！" << std::endl;
//     std::cout << "-----------------------------" << std::endl;
//     sigset_t pending;
//     sigpending(&pending);
//     PrintPending(pending);
//     std::cout << "-----------------------------" << std::endl;
// }
int cnt1 = 0,cnt2 = 0;
void handler1(int signo)
{
    cnt1++;
    std::cout << "cnt1 : " << cnt1 << std::endl;

}
void handler2(int signo)
{
    cnt2++;
    std::cout << "cnt2 : " << cnt2 << std::endl;
}
int main()
{
    signal(2, handler1);
    signal(40, handler2);
    sigset_t block_set, old_set;
    sigemptyset(&block_set);//全部置0
    sigemptyset(&old_set);
    sigaddset(&block_set, SIGINT); //把信号加入对应位置
    sigaddset(&block_set, SIGRTMIN+6); //把信号加入对应位置
    sigprocmask(SIG_BLOCK,&block_set,&old_set);//把设置好的位图设置进内核中，建立block表

    int cnt = 60;
    while (true)
    {
        sigset_t pending;
        sigpending(&pending);//初始化

        PrintPending(pending);
        cnt--;
        if (cnt == 0)
        {
            std::cout << "解除对2号信号和4号信号的屏蔽!!!" << std::endl;
            sigprocmask(SIG_UNBLOCK,&block_set,nullptr);
        }
        sleep(1);
    }
    return 0;
}